home *** CD-ROM | disk | FTP | other *** search
- (****************************************************************)
- (* InputEnv.mod *)
- (* Version 2.0 of InputEnv *)
- (* Versions 1.0, 1.1 written in C; *)
- (* Versions 2.0 and above written in Modula-2 *)
- (* Copyright © 1994 John E> Perry, /// *)
- (* This program and its source are freely distributable *)
- (****************************************************************)
- (* This program is a DOS interface, with prompting, for the *)
- (* user to set an environment variable. For example, I use it *)
- (* in my S:Shell-Startup file like so: *)
- (* *)
- (* InputEnv SUPPRESS SHELL UserName Please input your name: *)
- (* Prompt "$UserName @ *E[1m$Hostname*E[0m : *E[33;3m%S*E[0m> " *)
- (* *)
- (* which gives the prompt (special styles excluded :) *)
- (* *)
- (* jack@Sara : > *)
- (* *)
- (* for whatever shell I'm using. (Sara is the name of my Amiga.*)
- (****************************************************************)
- (* Suggestions, Bugs, Questions, Comments, Snide Remarks, Etc.: *)
- (* jep@nauvax.ucc.nau.edu *)
- (****************************************************************)
-
- MODULE InputEnv;
-
- (****************************************************************)
- (* Imported modules. *)
- (****************************************************************)
-
- FROM System IMPORT argv, argc;
- FROM SYSTEM IMPORT ADDRESS, ADR;
- FROM Terminal IMPORT WriteString, WriteLn, Write, Read;
- FROM Strings IMPORT ConcatString, CompareString, Relation, StringLength;
- FROM AmigaDOS2 IMPORT SetVar, GVBLocalOnly, GVBGlobalOnly,
- DOSVariableFlagsSet, GetVar;
-
- (****************************************************************)
- (* Programmer-defined constants. *)
- (* These are special characters that I need to define like so *)
- (* in order to achieve various desired effects with a Write. *)
- (* Or... defined for my convenience. *)
- (****************************************************************)
- CONST
- EOS = CHAR(0); (* end of string *)
- TAB = CHAR(9); (* tab *)
- EOL = CHAR(10); (* end of line *)
- ESC = CHAR(27); (* escape *)
-
- (****************************************************************)
- (* Module Variables. *)
- (* yesNo: an input variable (yes or no) *)
- (* counter: a string counter *)
- (* envVar: name of the environment variable being created *)
- (* envString: string AmigaDOS is to associate with envVar *)
- (* suppressFlag: suppress "...overwrite?" prompt? *)
- (* shellFlag: shell or globale environment variable? *)
- (* promptString: how to prompt user *)
- (* argString: used to point to command-line arguments *)
- (****************************************************************)
- VAR
- yesNo: CHAR;
- counter: CARDINAL;
- envVar: ARRAY[0..63] OF CHAR;
- envString: ARRAY[0..255] OF CHAR;
- suppressFlag, shellFlag: BOOLEAN;
- promptString: ARRAY[0..127] OF CHAR;
- argString: POINTER TO ARRAY [0..63] OF CHAR;
-
- (****************************************************************)
- (* PROCEDURE ReadLn(VAR string: ARRAY OF CHAR); *)
- (* Reads string until EOL is encountered. *)
- (****************************************************************)
- PROCEDURE ReadLn(VAR string: ARRAY OF CHAR);
- VAR
- in: CHAR; (* input character *)
- loc: CARDINAL; (* location in string *)
- BEGIN
- loc := 0;
- in := CHAR(0);
- (* read string *)
- WHILE in # CHAR(10)
- DO
- Read(in);
- string[loc] := in;
- INC(loc);
- END; (* WHILE *)
- string[loc - 1] := CHAR(0);
- END ReadLn;
-
- BEGIN
- IF argc = 1
- THEN
- WriteString('Dude! The usage is:'); WriteLn;
- Write(TAB); WriteString('InputEnv [SUPPRESS] [SHELL] <envname> <prompt>');
- WriteLn;
- WriteString('where <envname> is the name of the environment string and');
- WriteLn;
- WriteString('<prompt> is the prompt for the user. ');
- Write(ESC);
- WriteString('[3mNOTE THAT <prompt> MUST'); WriteLn;
- WriteString('BE THE LAST THING ON THE LINE, AND <envname> ');
- Write(ESC);
- WriteString('[1mMUST ');
- Write(ESC);
- WriteString('[22m BE NEXT TO LAST!!!');
- Write(ESC);
- WriteString('[23m');
- WriteLn;
- Write(TAB);
- WriteString('To suppress verification of new environment variable,');
- WriteLn;
- WriteString('use SUPPRESS, and to make a shell instead of a global');
- WriteLn;
- WriteString('environment variable, user SHELL.');
- WriteLn;
- WriteLn;
- WriteString('InputEnv v2.0 Copyright © 1994 John E> Perry, ///'); WriteLn;
- HALT;
- END; (* IF *)
- (* process command line *)
- counter := 1;
- argString := ADDRESS(argv^[1]);
- (* suppress "...overwrite?" prompt? *)
- IF CompareString(argString^, 'SUPPRESS') = equal
- THEN
- suppressFlag := TRUE;
- INC(counter);
- END; (* IF *)
- argString := ADDRESS(argv^[counter]);
- (* shell variable? default is global *)
- IF CompareString(argString^, 'SHELL') = equal
- THEN
- shellFlag := TRUE;
- INC(counter);
- END; (* IF *)
- ConcatString(envVar, argv^[counter]^);
- INC(counter);
- (* prompt *)
- WHILE counter <= (argc - 1)
- DO
- ConcatString(promptString, argv^[counter]^);
- ConcatString(promptString, ' ');
- INC(counter);
- END; (* WHILE *)
- (* get string *)
- WriteString(promptString);
- ReadLn(envString);
- (* check to see if variable exists *)
- IF NOT suppressFlag
- THEN
- IF NOT ((GetVar(ADR(envVar), ADR(promptString), 63, DOSVariableFlagsSet {GVBGlobalOnly}) = LONGINT-(1DH)) OR (GetVar(ADR(envVar), ADR(promptString), 63, DOSVariableFlagsSet {GVBLocalOnly}) = LONGINT-(1DH)))
- THEN
- WriteString('Variable already exists! overwrite? (Y/N) ');
- Read(yesNo);
- IF (yesNo = 'N') OR (yesNo = 'n')
- THEN
- HALT;
- END; (* IF *)
- END; (* IF *)
- END; (* IF *)
- (* okay, write it out *)
- IF shellFlag
- THEN
- shellFlag := SetVar(ADR(envVar), ADR(envString), LONGINT(StringLength(envString)), DOSVariableFlagsSet{GVBLocalOnly});
- ELSE
- shellFlag := SetVar(ADR(envVar), ADR(envString), LONGINT(StringLength(envString)), DOSVariableFlagsSet{GVBGlobalOnly});
- END; (* IF *)
- END InputEnv.
-